/*
 * @file      TableOutputTest.cpp
 * @brief     TableOutputTest is a test file to TableOutput interface.
 *
 * @author    Franco Riberi
 * @email     fgriberi AT gmail.com
 *
 * Contents:  Source file.
 *
 * System:    titere: Target Ideal TEmperature Rna Expression
 * Language:  C++
 *
 * @date February 3, 2015
 *
 * Copyright (C) 2015 Franco Riberi, FuDePAN
 *
 * This file is part of titere.
 *
 * titere is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * titere is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with titere. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <fstream>
#include <biopp/biopp.h>
#include <gtest/gtest.h>

#define private public
#include "titere/TableOutput.h"
#include "titere/Exceptions.h"

using namespace NSTitere;

static const char* FILE_NAME = "testFile.csv";

TEST(TableOutputSuiteTest, generateHeader)
{
    TableOutput output(FILE_NAME);

    const std::string testText = "Sequence, Position, Temperature, #unpaired, #unpaired2-8, Comparison";
    std::ifstream tmpIn;
    std::string fromFile;
    tmpIn.open(FILE_NAME);
    std::getline(tmpIn, fromFile);
    tmpIn.close();

    EXPECT_EQ(testText, fromFile);
}

TEST(TableOutputSuiteTest, save)
{
    TableOutput output(FILE_NAME);

    const std::string testText  = "Sequence, Position, Temperature, #unpaired, #unpaired2-8, Comparison";
    const std::string lineOne   = "U71132.1, 1, 38, 21, 2, GGCTATGGGTTTGGGC";
    const std::string lineTwo   = "U71132.1, 1, 39, 15, 5, GTTCCAGTATGCCCCC";
    const std::string lineThree = "U71132.1, 1, 40, 12, 1, GTTCCAGTATGCCTTT";

    output.save("U71132.1", 1u, 38, 21u, 2u, "GGCTATGGGTTTGGGC");
    output.save("U71132.1", 1u, 39, 15u, 5u, "GTTCCAGTATGCCCCC");
    output.save("U71132.1", 1u, 40, 12u, 1u, "GTTCCAGTATGCCTTT");

    std::string headerLine;
    std::string firstLineValue;
    std::string secondLineValue;
    std::string thirdLineValue;

    std::ifstream tmpIn;
    tmpIn.open(FILE_NAME);
    std::getline(tmpIn, headerLine);
    std::getline(tmpIn, firstLineValue);
    std::getline(tmpIn, secondLineValue);
    std::getline(tmpIn, thirdLineValue);
    tmpIn.close();

    EXPECT_EQ(testText, headerLine);
    EXPECT_EQ(lineOne, firstLineValue);
    EXPECT_EQ(lineTwo, secondLineValue);
    EXPECT_EQ(lineThree, thirdLineValue);
}

TEST(TableOutputSuiteTest, fileNotOpen)
{
    TableOutput output(FILE_NAME);
    output._oFile.close();
    EXPECT_THROW(output.save("U71132.1", 1u, 38, 21u, 2u, "GGCTATGGGTTTGGGC"), FileNotOpened);
}